<template>

	<h1>登陆</h1>
	<hr>
	<input type="text" v-model="userForms.username" />
	<input type="text" v-model="userForms.password" />


	<button class="login-btn" @click="login">登陆</button>
	<p>
		还未注册？ 点击立即 <router-link to="/reg">注册</router-link>
	</p>

</template>
<script setup>
	import {
		ref,
		reactive,
		onMounted,
		toRefs
	} from 'vue';


	import {
		loginApi
	} from '@/api/user.js'
	import router from '@/router'
	import {useRoute} from 'vue-router'

	import {
		showToast
	} from 'vant';
import useUserStore from '@/store/userStore';

const route = useRoute()




	const userForms = reactive({
		username: '',
		password: ''
	})



	function login() {


		loginApi(userForms).then(res => {
			console.log(res)
			if (res.code == 0) {

				showToast(res.msg)
				
				let {tokenExpired,token,userInfo} = res;
				
				
				
				localStorage.setItem('token',token)
				localStorage.setItem('tokenExpired',tokenExpired)
				// sessionStorage.setItem('userInfo',JSON.stringify(userInfo))

				//  存储  
				// localStorage token 
				// session  用户信息 userInfo

				// 请求用户信息
				
				
				
				let {isLogin,uploadUserInfo} =  toRefs(useUserStore())
								
								
									uploadUserInfo.value(res.userInfo)
									isLogin.value = true

				setTimeout(v => {
                  if(route.query.come){
					 router.back() 
				  }else{
					  router.replace('/')
				  }
					
				}, 1000)

			} else {

				showToast(res.msg)
			}
		})
	}
</script>
<style>
	@import url('../user.css');
</style>